<?php
namespace tp5_lib\access\test;

use think\testing\TestCase;
use tp5_lib\access\logic\PrivilegeLogic;

/** 测试授权和访问控制的匹配 */
class PrivilegeTest extends TestCase
{
    /** 一级授权 */
    public function testPrimary()
    {
        $logic=PrivilegeLogic::instance();
        $logic->clear();

        $this->assertFalse($logic->check("test"));
        $this->assertFalse($logic->check("test.main"));

        $logic->accredit("test");
        $this->assertTrue($logic->check("test"));
        $this->assertTrue($logic->check("test.main"));
        $this->assertTrue($logic->check("test.main.index"));
        $this->assertFalse($logic->check("demo"));
    }
    /** 二级授权 */
    public function testSecondary()
    {
        $logic=PrivilegeLogic::instance();
        $logic->clear();

        $logic->accredit("test.main");
        $this->assertFalse($logic->check("test"));
        $this->assertTrue($logic->check("test.main"));
        $this->assertTrue($logic->check("test.main.index"));
        $this->assertFalse($logic->check("test.other"));
    }
    /** `&`和`|`连接的权限 */
    public function testOperators()
    {
        $logic=PrivilegeLogic::instance();
        $logic->clear();
        $this->assertFalse($logic->check("test.other&demo.main"));
        $this->assertFalse($logic->check("test.other|demo.main"));

        $logic->accredit("test.other");
        $this->assertFalse($logic->check("test.other&demo.main"));
        $this->assertTrue($logic->check("test.other|demo.main"));
    }
    /** 多个`&`和`|`连接的权限 */
    public function testMultiOperators()
    {
        $logic=PrivilegeLogic::instance();
        $logic->clear();
        $logic->accredit("test.1,test.2,test.3");
        $this->assertTrue($logic->check("test.1&test.2&test.3"));
        $this->assertFalse($logic->check("test.1&test.2&test.4"));
        $this->assertTrue($logic->check("test.1&test.2|test.4"));
        $this->assertFalse($logic->check("test.1&test.4|test.4"));
        $this->assertTrue($logic->check("test.1&test.4|test.3"));
        $this->assertTrue($logic->check("test.7|test.2|test.5"));
        $this->assertFalse($logic->check("test.7|test.9|test.5"));
    }
    /** 多个授权 */
    public function testMultiAccredit()
    {
        $logic=PrivilegeLogic::instance();
        $logic->clear();
        $logic->accredit("test.other");
        $logic->accredit("demo.main");
        $this->assertTrue($logic->check("test.other&demo.main"));
    }
    /** `,`连接的多个授权 */
    public function testAccreditConnector()
    {
        $logic=PrivilegeLogic::instance();
        $logic->clear();
        $logic->accredit("test.other,demo.main");
        $this->assertTrue($logic->check("test.other&demo.main"));
        $this->assertTrue($logic->check("test.other|demo.main"));
    }
    /** `+`,`-`修饰的授权 */
    public function testAccreditModifier()
    {
        $logic=PrivilegeLogic::instance();
        $logic->clear();
        $logic->accredit("test,-test.main,test.main.index,+test.main.other");
        $this->assertTrue($logic->check("test"));
        $this->assertFalse($logic->check("test.main"));
        $this->assertFalse($logic->check("test.main.index"));
        $this->assertTrue($logic->check("test.main.other"));
    }
}
